package com.wangyk.rabbitmqcommon.fdfs;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Configuration
@PropertySource("classpath:common.properties")
public class FileFactory
{
    @Value("${flag}")
    private String flag;

    // endpoint以杭州为例，其它region请按实际情况填写
    @Value("${endpoint}")
    private String endpoint;

    // accessKey请登录https://ak-console.aliyun.com/#/查看
    @Value("${accessKeyId}")
    private String accessKeyId;

    @Value("${secretAccessKey}")
    private String secretAccessKey;

    @Value("${group}")
    private String group;

    @Value("${bucketName}")
    private String bucketName;

    @Value("${tracker_server}")
    private String tracker_server;

    public String getTracker_server()
    {
        return tracker_server;
    }

    public void setTracker_server(String tracker_server)
    {
        this.tracker_server = tracker_server;
    }

    public String getGroup()
    {
        return group;
    }

    public void setGroup(String group)
    {
        this.group = group;
    }

    public String getBucketName()
    {
        return bucketName;
    }

    public void setBucketName(String bucketName)
    {
        this.bucketName = bucketName;
    }

    public String getEndpoint()
    {
        return endpoint;
    }

    public void setEndpoint(String endpoint)
    {
        this.endpoint = endpoint;
    }

    public String getAccessKeyId()
    {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId)
    {
        this.accessKeyId = accessKeyId;
    }

    public String getSecretAccessKey()
    {
        return secretAccessKey;
    }

    public void setSecretAccessKey(String secretAccessKey)
    {
        this.secretAccessKey = secretAccessKey;
    }

    public String getFlag()
    {
        return flag;
    }

    public void setFlag(String flag)
    {
        this.flag = flag;
    }

    /**
     * @return
     * @Description 获取客户端实例
     */
    public IFileClient creator()
    {
        if ("1".equals(flag))
        {
            return new FastDFSClient(group, tracker_server);
        }
        else
        {
            return new OssClient(endpoint, accessKeyId, secretAccessKey, bucketName);
        }

    }

    /**
     * @return
     * @Description 获取完整url路径
     */
    public String getUrl(String uri)
    {
        if (null == uri)
        {
            uri = "";
        }
        if ("1".equals(flag))
        {
            return group + uri;
        }
        else
        {
            StringBuffer sb = new StringBuffer();

            try
            {
                uri = URLEncoder.encode(uri, "utf-8");
            }
            catch (UnsupportedEncodingException e)
            {
            }
            sb = new StringBuffer();
            sb.append("https://");
            sb.append(bucketName);
            sb.append(".");
            sb.append(endpoint);
            sb.append("/");
            sb.append(uri);
            String yuming = sb.toString();

            return yuming;
        }

    }

}
